package base.shejimoshi;

// 单例模式---饿汉式
public class Singleton {
    private static final Singleton singleton = new Singleton();

    private Singleton() {

    }

    // 静态方法
    public static Singleton getInstance() {
        return singleton;
    }
}


// 单例模式---懒汉式
class Singleton1 {
    private static volatile Singleton1 singleton1 = null;

    private Singleton1() {

    }

    // 静态方法
    public static Singleton1 getInstance() {
        if (singleton1 == null) {
            synchronized (Singleton1.class) {
                if (singleton1 == null) {
                    singleton1 = new Singleton1();
                }
            }
        }
        return singleton1;
    }
}